package com.example.easydownloadback.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.easydownloadback.domin.File;
import com.example.easydownloadback.domin.ResponseResult;
import com.example.easydownloadback.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/admin/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public String upload(@RequestParam MultipartFile file) {
        return fileService.upload(file);
    }
    @PostMapping("/upload/picture")
    public String uploadPicture(@RequestParam MultipartFile file) {
        return fileService.uploadPicture(file);
    }
    @GetMapping("/download/{fileName}")
    public void downLoad(@PathVariable String fileName, HttpServletResponse response) {
        fileService.downLoadFile(fileName, response);
    }
    @GetMapping("/download/picture/{uuFile}")
    public void downLoadPicture(@PathVariable String uuFile, HttpServletResponse response) {
        fileService.downLoadPicture(uuFile, response);
    }

    @PostMapping
    public ResponseResult<String> addFile(@RequestBody File file) {
        return fileService.addFile(file);
    }

    @GetMapping("/page")
    public ResponseResult<IPage<File>> listPage(
            @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,
            @RequestParam("selectValue") String selectValue, @RequestParam String partition) {
        if (StrUtil.isBlank(selectValue) && StrUtil.isBlank(partition)) {
            return fileService.listPage(pageNum, pageSize);
        }
        return fileService.listPageBySelectAndType(pageNum, pageSize, selectValue, partition);
    }
    @DeleteMapping("/{id}")
    public ResponseResult deleteFileById(@PathVariable("id") String id) {
        return fileService.deleteFileById(id);
    }
    @GetMapping("/info/{id}")
    public ResponseResult<File> getFileById(@PathVariable("id") String id) {
        return fileService.getFileById(id);
    }
    @PutMapping
    public ResponseResult updateFile(@RequestBody File file) {
        return fileService.updateFile(file);
    }
    @PostMapping("/batch")
    public ResponseResult deleteFileBatch(@RequestBody String[] ids) {
        return fileService.deleteFileBatch(ids);
    }
}
